﻿package com.wly.ui.gamescene
{
	import com.wly.sound.SoundManager;
	import com.wly.ui.common.GlobalInfo;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class DrawItem extends Sprite implements IHitObject
	{
		public var startPos:Point;

		protected var _angle:Number;
		protected var _drawRectX:Number;
		protected var _drawRectY:Number;
		protected var _drawRectW:Number;
		protected var _drawRectH:Number;
		
		static protected const DRAW_HEIGHT:int = 9;
		
		public function DrawItem()
		{
			super();
			
			_angle = 0;
			_drawRectH = 0;
			_drawRectW = 0;
			_drawRectX = 0;
			_drawRectY = 0;
		}
		
		public function get drawX():Number
		{
			return _drawRectX;
		}
		
		public function get drawY():Number
		{
			return _drawRectY;
		}
		
		public function get drawW():Number
		{
			return _drawRectW;
		}
		
		public function get drawH():Number
		{
			return _drawRectH;
		}
		
		public function get angle():Number
		{
			return _angle;
		}
		
		public function drawTo(endPos:Point):void
		{
			graphics.clear();
			
			if(!startPos)
			{
				return;
			}
			
			var leftPos:Point    = startPos.x<endPos.x?startPos:endPos;
			var rightPos:Point   = startPos.x<endPos.x?endPos:startPos;
			
			var drawWidth:Number = Point.distance(leftPos, rightPos);
			_angle = Math.asin((rightPos.y-leftPos.y)/drawWidth);
			
			graphics.beginFill(0xFF0000);
			graphics.drawRoundRect(-drawWidth/2, -DRAW_HEIGHT/2, drawWidth, DRAW_HEIGHT, 2);
			graphics.endFill();
			x = leftPos.x+drawWidth/2;
			y = (leftPos.y+rightPos.y+DRAW_HEIGHT)/2;
			rotation = _angle*180/Math.PI;
			
			_drawRectX = x;
			_drawRectY = y;
			_drawRectW = drawWidth;
			_drawRectH = DRAW_HEIGHT;
		}
		
		public function onHit(obj:DisplayObject, hitPos:Point):Boolean
		{
			SoundManager.instance.playEffectSound(SoundManager.SpringSound);
			return true;
			if(obj is Player)
			{
				var palyer:Player = obj as Player;
				palyer.applyForcy(GlobalInfo.ROPE_FORCE*Math.sin(_angle), GlobalInfo.ROPE_FORCE*Math.cos(_angle));
			}
			
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			
			return false;
		}
	}
}